package com.hp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.entity.UserRole;
import com.hp.service.UserRoleService;
import com.hp.util.JsonData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping("/userRole")
public class UserRoleController {

    @Resource
    private UserRoleService userRoleService;

    @RequestMapping("/list")
    @ResponseBody
    public Object list(UserRole userRole, @RequestParam(value = "page",defaultValue = "1")Integer page,
                       @RequestParam(value = "limit",defaultValue = "10")Integer limit) {
        PageHelper.startPage(page,limit);
        List<UserRole> list = userRoleService.list(userRole);
        return JsonData.buildSuccess(new PageInfo<>(list));
    }

    @RequestMapping("/list2")
    @ResponseBody
    public Object list2(UserRole userRole, @RequestParam(value = "page",defaultValue = "1")Integer page,
                       @RequestParam(value = "limit",defaultValue = "10")Integer limit) {
        PageHelper.startPage(page,limit);
        List<UserRole> list2 = userRoleService.list2(userRole);
        return JsonData.buildSuccess(new PageInfo<>(list2));
    }
    //删除
    @RequestMapping("/del")
    @ResponseBody
    public Object del(Integer id) {
        int i = userRoleService.del(id);
        return JsonData.buildSuccess(i>0?"删除成功！":"删除失败！");
    }

    //新增
    @RequestMapping("/add")
    @ResponseBody
    public Object add(UserRole userRole) {
        int i= userRoleService.add(userRole);
        return JsonData.buildSuccess(i>0?"新增成功！":"新增失败！");
    }

    //通过findById查询
    @RequestMapping("/findById")
    @ResponseBody
    public Object findById(Integer id) {
        UserRole userRole = userRoleService.findById(id);
        return JsonData.buildSuccess(userRole);
    }
    //修改
    @RequestMapping("/update")
    @ResponseBody
    public Object update(UserRole userRole) {
        int i = userRoleService.update(userRole);
        return JsonData.buildSuccess(i>0?"修改成功！":"修改失败！");
    }
}